package cn.lanqiao.springboot.controller;

import cn.lanqiao.springboot.entity.Role;
import cn.lanqiao.springboot.service.RoleService;
import cn.lanqiao.springboot.util.Result;
import cn.lanqiao.springboot.vo.InsertOrUpdateRoleVO;
import cn.lanqiao.springboot.vo.QueryRoleVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @PostMapping("/list")
    public Result getList(@RequestBody QueryRoleVO queryRoleVO) {
        PageHelper.startPage(queryRoleVO.getCurrentPage(), queryRoleVO.getPageSize());
        List<Role> roleList = roleService.getList(queryRoleVO);
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);
        return Result.success(pageInfo);
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody InsertOrUpdateRoleVO insertOrUpdateRoleVO) {
        roleService.insert(insertOrUpdateRoleVO);
        return Result.success();
    }

    @PostMapping("/update")
    public Result update(@RequestBody InsertOrUpdateRoleVO insertOrUpdateRoleVO) {
        roleService.update(insertOrUpdateRoleVO);
        return Result.success();
    }
}
